var fs = require('fs');
var path = require('path');
var fileUtil = require('./modules/sys/fileUtil');

var src = path.join(__dirname, 'init');
var dst = process.cwd();
var bakdst = dst + '/备份文件';

module.exports = {
    init: () => {
        console.log('项目文件初始化开始！');
        copydir(src, dst, bakdst, {
            count: 0
        }, fs.existsSync(path.join(dst, 'app.js')));
    },

    update: () => {
        console.log('项目文件更新开始！');
        copydir(src, dst, bakdst, {
            count: 0
        }, false);
    }
};

function copydir(src, dst, bakdst, countObj, iscover) {
    fs.readdir(src, function (err, paths) {
        if (err) {
            console.error(err);
            return;
        }

        paths.forEach(function (item) {
            var _src = src + '/' + item,
                _dst = dst + '/' + item,
                _bakdst = bakdst + '/' + item,
                readable, writable;

            fileUtil.mkdirs(iscover ? bakdst : dst, function () {
                fs.stat(_src, function (err, st) {
                    if (err) {
                        console.error(err);
                        return;
                    }

                    if (st.isFile()) {
                        if (['index.html', 'login.html', 'config.js', 'index.js', 'login.js', 'access.js', 'content.js', 'loginMapper.js', 'loginService.js', 'app.js'].includes(path.basename(_src))) {
                            if (fs.existsSync(_dst)) {
                                if (iscover) {
                                    fs.renameSync(_dst, _bakdst);
                                    console.log('文件' + item + '已备份！');
                                } else {
                                    console.log('文件' + item + '已存在！');
                                    countObj.count++;
                                    if (countObj.count == 23) {
                                        console.log('文件拷贝完成！');
                                        process.exit();
                                    }
                                    return;
                                }
                            }
                        }
                        readable = fs.createReadStream(_src);
                        writable = fs.createWriteStream(_dst);
                        writable.on('finish', () => {
                            console.log('文件' + item + '已拷贝！');
                            countObj.count++;
                            if (countObj.count == 23) {
                                console.log('文件拷贝完成！');
                                process.exit();
                            }
                        });
                        readable.pipe(writable);
                    } else if (st.isDirectory()) {
                        fileUtil.mkdirs(_dst, function () {
                            copydir(_src, _dst, _bakdst, countObj, iscover);
                        });
                    }
                });
            });
        });
    });
}